<?php

/**
 * Default Text class
 *
 * Provides some methods for converting and recognizing the encoding, as well as other common text functions such as truncate, humanize etc
 */
class Text {

	/**
	 * Returns a truncated text string with the specified length
	 *
	 * @param string text the text to be truncated
	 * @param integer length the truncated text length 
	 */
	public static function truncate($text, $length) {
		return ( Text::isUTF8($text) ) ? mb_substr($text, 0, $length, 'UTF-8') : substr($text, 0, $length);	
	}
	
	/**
	 * Returns the length of the specified text
	 * 
	 * @param string text
	 */
	public static function length($text) {
		return ( Text::isUTF8($text) ) ? mb_strlen($text, 'UTF-8') : strlen($text);
	}
	
	/**
	 * Returns a summarized version of the specified string
	 */
	public static function summarize($text, $length=75, $more='&hellip;') {
		$text = preg_replace('/^https?:\/\/(www\.)?/i', '', $text);
		$text = strip_tags($text);
		if ( $length < Text::length($text) ) {
			$text = Text::truncate($text, $length);
			$text = $text . $more;
		}
		return $text;
	}
	
	/**
	 * Returns a human-readable version of the specified string. It can be either camelCased or underscore-syntaxed.
	 * 
	 * @param string text the text to be humanized
	 * @return a human-readable version of the specified string
	 */
	public static function humanize($text) {
		$text = preg_replace('/([A-Z])/', '_\\1', $text);	// camelCased
		$text = str_replace('_', ' ', $text);	// underscore-syntax
		$text = ucwords($text);
		return $text;
	}
	
	/**
	 * Returns a camelCased version of the speciife string. It can be either human-readable or underscore-syntax
	 *
	 * @param string text the text to be camelized
	 * @return a camelCased version of the specified string
	 */	 
	public static function camelize($text) {
		$text = str_replace('_', ' ', $text);
		$text = ucwords($text);
		$text = str_replace(' ', '', $text);
		return $text;
	}
	
	/**
	 * Returns true if the specified text is encoded in UTF-8 format
	 *
	 * @return true only and only if the specified text is encoded in UTF-8 format
	 */
	public static function isUTF8($text) {
	   // From http://w3.org/International/questions/qa-forms-utf-8.html
	   return preg_match('%^(?:
			 [\x09\x0A\x0D\x20-\x7E]            # ASCII
		   | [\xC2-\xDF][\x80-\xBF]            # non-overlong 2-byte
		   |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
		   | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
		   |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
		   |  \xF0[\x90-\xBF][\x80-\xBF]{2}    # planes 1-3
		   | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
		   |  \xF4[\x80-\x8F][\x80-\xBF]{2}    # plane 16
	   )*$%xs', $text);	
	}
	
	/**
	 * Converts the specified text encoding
	 *
	 * @param string text the text to be used
	 * @param string from the old encoding
	 * @param stirng to the new encoding
	 */
	public static function convertEncoding($text, $from, $to) {
		return mb_convert_encoding($text, $to, $from);
	}
}

?>